package com.rongshu.view.login.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.adapter.LoginQuestionAdapter;
import com.rongshu.view.login.contract.LoginQuestionView;
import com.rongshu.view.login.entity.LoginQuestion;
import com.rongshu.view.login.presenter.LoginQuestionPresenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginQuestionActivity extends BaseActivity implements LoginQuestionView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    //presenter
    private LoginQuestionPresenter mLoginQuestionPresenter;

    //adapter
    private LoginQuestionAdapter mLoginQuestionAdapter = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_login_question_view;
    }

    @Override
    protected void initView() {
        //设置title
        txt_top_title.setText("反馈与帮助");
        //presenter
        mLoginQuestionPresenter = new LoginQuestionPresenter(this);
        //设置RecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(manager);
    }

    @Override
    protected void initData() {
        //获取登录问题
        Map<String, Object> map = new HashMap<>();
        map.put("rnd", 0);
        mLoginQuestionPresenter.getLoginProblems(
                (int) map.get("rnd"),
                Common.encryptionSign(map));
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * 获取登录遇到的问题
     *
     * @param loginQuestion
     */
    @Override
    public void loginProblems(LoginQuestion loginQuestion) {
        if (loginQuestion.getErrCode() == 0) {

            List<LoginQuestion.ResponseBean> responseBeans = loginQuestion.getResponse();
            if (responseBeans == null) {
                return;
            }

            if (mLoginQuestionAdapter == null) {
                mLoginQuestionAdapter = new LoginQuestionAdapter(responseBeans,this);
                mRecyclerView.setAdapter(mLoginQuestionAdapter);
            } else {
                mLoginQuestionAdapter.setNewData(responseBeans);
                mLoginQuestionAdapter.notifyDataSetChanged();
            }
        } else {
            toast(loginQuestion.getErrMsg());
        }
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }
}
